package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.domain.entity.SysUserDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EmpDepRelationship;
import com.ruoyi.system.mapper.EmpDepRelationshipMapper;
import com.ruoyi.system.mapper.SysUserDeptMapper;
import com.ruoyi.system.service.IEmpDepRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class EmpDepRelationshipServiceImpl implements IEmpDepRelationshipService {

    @Autowired
    private EmpDepRelationshipMapper empDepRelationshipMapper;

    @Autowired
    private SysUserDeptMapper userDeptMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEmpDepBatch(List<EmpDepRelationship> empDepRelationshipList) {
        int rows = 0;
        SysUserDept sysUserDept = null;
        List<SysUserDept> userDeptList = new ArrayList<>();
        for (EmpDepRelationship empDepRelationship : empDepRelationshipList) {
            sysUserDept = new SysUserDept();
            sysUserDept.setUserId(Long.parseLong(empDepRelationship.getEmplId()));
            sysUserDept.setDeptId(Long.parseLong(empDepRelationship.getDeptId()));
            if (StringUtils.isNotNull(empDepRelationship.getHrStatus())) {
                sysUserDept.setHrStatus(empDepRelationship.getHrStatus());
            } else {
                sysUserDept.setHrStatus("A");//如果为空，则默认有效
            }
            if (StringUtils.isNotNull(empDepRelationship.getEmplRcd())) {
                sysUserDept.setEmplRcd(empDepRelationship.getEmplRcd());
            } else {
                sysUserDept.setEmplRcd("");
            }
            userDeptList.add(sysUserDept);
        }
        rows += userDeptMapper.insertOrUpdateUsreDept(userDeptList);
        return rows;
    }
}
